using System;
using System.IO;
using System.Threading;

namespace MEA.AE_Data.Access
{
	public static class FileIO
	{
		private static string logpath = "";

		private static Thread threadStandard;

		private static MemoryStream stream;

		public static MemoryStream Stream
		{
			get
			{
				return stream;
			}
		}

		public static void WriteStreamToFile(MemoryStream mstream, string path)
		{
			stream = mstream;
			logpath = path;
			if (threadStandard != null)
			{
				threadStandard.Abort();
			}
			threadStandard = new Thread(TH_WriteToFile);
			threadStandard.Name = "TH_WriteToFile";
			threadStandard.Start();
		}

		private static void TH_WriteToFile()
		{
			FileStream fileStream = null;
			int num = (int)stream.Length;
			try
			{
				File.Delete(logpath);
			}
			catch
			{
				return;
			}
			try
			{
				fileStream = File.OpenWrite(logpath);
				stream.Position = 0L;
				for (int i = 0; i < num; i++)
				{
					fileStream.WriteByte((byte)stream.ReadByte());
				}
				fileStream.Flush();
				fileStream.Close();
			}
			catch (Exception)
			{
				try
				{
					if (fileStream != null)
					{
						fileStream.Close();
					}
				}
				catch
				{
				}
			}
		}

		public static void ReadFileToStream(string path)
		{
			logpath = path;
			if (threadStandard != null)
			{
				threadStandard.Abort();
			}
			threadStandard = new Thread(TH_ReadFileToStream);
			threadStandard.Name = "TH_ReadFileToStream";
			threadStandard.Start();
		}

		private static void TH_ReadFileToStream()
		{
			FileStream fileStream = File.OpenRead(logpath);
			stream = new MemoryStream();
			stream.SetLength(fileStream.Length);
			fileStream.Read(stream.GetBuffer(), 0, (int)fileStream.Length);
			stream.Flush();
			fileStream.Close();
		}
	}
}
